//
//  QDTicklingController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/21.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDTicklingController.h"

@interface QDTicklingController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(strong,nonatomic) NSArray<NSString *> *imgArr;

@property(strong,nonatomic) NSArray<NSString *> *massegeArr;

@property(weak,nonatomic) UITextView *textView;

@property(weak,nonatomic) UILabel *label;

@property(weak,nonatomic) UILabel *lblCount;

@property(weak,nonatomic) UITableView *table;

@end

@implementation QDTicklingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:65/255.0 green:197/255.0 blue:174/255.0 alpha:1];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpUI];
    
}
#pragma mark
#pragma mark - 搭建界面
- (void) setUpUI
{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    [self.view addSubview:tableView];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.tableHeaderView = [self createHeaderView];
    
    self.table = tableView;
    
    self.imgArr = @[@"dianhua",@"qq"];
    
    self.massegeArr = @[@"官服电话 :400-636-6113",@"用户交流群  :QQ 464768569"];
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
}

#pragma mark
#pragma mark - 设置组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return @"欢迎联系我们";
    
}

#pragma mark
#pragma mark - 实现数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.massegeArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.imgArr[indexPath.row]];
    
    cell.textLabel.text = self.massegeArr[indexPath.row];
    
    return cell;
    
    
    
}

#pragma mark
#pragma mark - 设置表头
- (UIView *)createHeaderView
{
    
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    
    viewHeader.backgroundColor = [UIColor whiteColor];
    
    UITextView *textView = [[UITextView alloc] init];
    
    [viewHeader addSubview:textView];
    
    textView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    self.textView = textView;
    
    textView.delegate = self;
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.equalTo(viewHeader).mas_offset(10);
        
        make.trailing.equalTo(viewHeader).mas_offset(-10);
        
        make.height.mas_equalTo(150);
        
    }];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 3, 200, 20)];
    
    label.enabled = NO;
    
    label.text = @"请输入反馈信息";
    
    label.font =  [UIFont systemFontOfSize:15];
    
    label.textColor = [UIColor lightGrayColor];
    
    [textView addSubview:label];
    
    self.label = label;
    
    UIButton *button = [[UIButton alloc] init];
    
    button.layer.masksToBounds = YES;
    
    button.layer.cornerRadius = 5;
    
    [viewHeader addSubview:button];
    
    [button setTitle:@"提交反馈" forState:UIControlStateNormal];
    
    [button setBackgroundColor:[UIColor colorWithRed:23/255.0 green:188/255.0 blue:187/255.0 alpha:1]];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(textView);
        
        make.top.equalTo(textView.mas_bottom).mas_offset(5);
        
        make.bottom.equalTo(viewHeader).mas_equalTo(-5);
        
        make.width.mas_equalTo(80);
        
    }];
    
    [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *lblCount = [[UILabel alloc] init];
    
    lblCount.text = @"您还可以输入的字数为 :200";
    
    [viewHeader addSubview:lblCount];
    
    [lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(textView.mas_bottom).mas_offset(5);
        
        make.bottom.equalTo(viewHeader).mas_equalTo(-5);
        
        make.leading.equalTo(textView).mas_offset(50);
        
    }];
    
    self.lblCount = lblCount;
    
    
    
    
    return viewHeader;
}

#pragma mark
#pragma mark - 实现textView的数据源方法
- (void)textViewDidChange:(UITextView *)textView
{
    
    if ([textView.text length] == 0) {
        
        [self.label setHidden:NO];

    }else{
        
        [self.label setHidden:YES];
        
    }
    
    NSInteger count = [textView.text length];
         self.lblCount.text = [NSString stringWithFormat:@"您还可以输入的字数为 :%ld",200-count];

    
<<<<<<< HEAD
=======
    if(count > 200)
    {
        self.lblCount.text = [NSString stringWithFormat:@"您还可以输入的字数为 :0"];
//        textView.editable = NO;
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 200)];
        return;
    }
    else
    {
        self.lblCount.text = [NSString stringWithFormat:@"您还可以输入的字数为 :%ld",200-count];
    }
>>>>>>> 76f7ebb809f910eb60cd8149f6d769fca086331b
    
}

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView

{
    
    if(textView.tag == 0) {
        
        textView.text = @"";
        
        textView.textColor = [UIColor blackColor];
        
        textView.tag = 1;
        
    }
    
    return YES;
    
}

#pragma mark
#pragma mark - 退出键盘(未实现)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    //取消第一响应者
    //[self.table resignFirstResponder];
    
    //[self.table endEditing:YES];
    
}

#pragma mark
#pragma mark - 实现点击button跳回
- (void)clickButton
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



@end
